{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Batch processing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The approach to batch processing depends on the structure of the anybody model. A commonly used strategy is a single anyscript main file. The main file is then loaded with different define statements to set the trial name, subject id or other parameters. This strategy for batch processing is outlined on the [anyscript.org wiki page](http://wiki.anyscript.org/index.php/Running_several_files_as_batch).\n",
    "\n",
    "Another strategy is to have a separate main file for each trial. These individual main-files hold all trial-specific data and include a base model that is common to all trials and subjects. The approach makes it much easier to work with mocap models that have many subject or hundreds of trials. \n",
    "\n",
    "The following shows how to batch process models where each trials is its own main file. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model with many main files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This ([zip](Tutorial_files/BatchProcessExample.zip)) illustrates the concept of a single model with many main files. The model is based on the Arm2D demo from the AnyBody tutorials. The main files are located in subfolders (`model1`-`model9`). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Volume in drive C has no label.\n",
      " Volume Serial Number is CE8A-96EC\n",
      "\n",
      " Directory of C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\n",
      "\n",
      "[.]               [model1]          [model5]          [model9]\n",
      "[..]              [model2]          [model6]          [Output]\n",
      "batchprocess.py   [model3]          [model7]          RunExample.bat\n",
      "Demo.Arm2D.any    [model4]          [model8]          \n",
      "               3 File(s)         10.870 bytes\n",
      "              12 Dir(s)  83.615.764.480 bytes free\n"
     ]
    }
   ],
   "source": [
    "!dir /d  \"Tutorial_files/BatchProcess\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\\model1\\main.any\n",
      "C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\\model2\\main.any\n",
      "C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\\model3\\main.any\n",
      "C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\\model4\\main.any\n",
      "C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\\model5\\main.any\n",
      "C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\\model6\\main.any\n",
      "C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\\model7\\main.any\n",
      "C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\\model8\\main.any\n",
      "C:\\Users\\mel\\Documents\\AnyBodySource\\AnyPyTools\\docs\\Tutorial\\Tutorial_files\\BatchProcess\\model9\\main.any\n"
     ]
    }
   ],
   "source": [
    "!dir /s /b \"main.any\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each of main files `main.any` has it own set of parameters, and they all include the same common fiile `Demo.Arm2D.any`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0f2c2eb9595046978fcb835589f7c5e1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/9 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Completed: 9\n"
     ]
    }
   ],
   "source": [
    "from anypytools import AnyPyProcess\n",
    "\n",
    "folderlist = [\"Tutorial_files/BatchProcess\"]\n",
    "app = AnyPyProcess(num_processes = 3)\n",
    "macrolist = [\n",
    "    'load \"main.any\"',\n",
    "    'operation Main.Study.InverseDynamics',\n",
    "    'run'\n",
    "]\n",
    "\n",
    "app.start_macro(macrolist, folderlist, search_subdirs= \"model[1-9].*main.any\" );"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "025ef9b0e7f3484faf153ecb56d6ae68": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "0f2c2eb9595046978fcb835589f7c5e1": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "children": [
        "IPY_MODEL_15c3edb098b14c28819b20b7a50e640a",
        "IPY_MODEL_173fcd5a3d914ba1a3487bdd86adde70",
        "IPY_MODEL_a154eb85c9c34831bf3796f044adf0af"
       ],
       "layout": "IPY_MODEL_025ef9b0e7f3484faf153ecb56d6ae68"
      }
     },
     "15c3edb098b14c28819b20b7a50e640a": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_3c8fca072d99418ea579550e2b50ce62",
       "style": "IPY_MODEL_db2c7be2f1c6471c876dafeffb4b9f68",
       "value": "100%"
      }
     },
     "173fcd5a3d914ba1a3487bdd86adde70": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "bar_style": "success",
       "layout": "IPY_MODEL_a257ba9983524ff48b818a3819d21f4a",
       "max": 9,
       "style": "IPY_MODEL_17e79745bba84620874a8166423132d1",
       "value": 9
      }
     },
     "17e79745bba84620874a8166423132d1": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "222fbecdea744b9da9ee1cdbb40105bc": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "3c8fca072d99418ea579550e2b50ce62": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "79629d3e114049178ff848db92194800": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "a154eb85c9c34831bf3796f044adf0af": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_222fbecdea744b9da9ee1cdbb40105bc",
       "style": "IPY_MODEL_79629d3e114049178ff848db92194800",
       "value": " 9/9 [00:06&lt;00:00,  1.61it/s]"
      }
     },
     "a257ba9983524ff48b818a3819d21f4a": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "db2c7be2f1c6471c876dafeffb4b9f68": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
